package com.zhiyou100.controller.admin;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Course;
import com.zhiyou100.model.Speaker;
import com.zhiyou100.model.Vi;
import com.zhiyou100.model.Video;
import com.zhiyou100.service.CourseService;
import com.zhiyou100.service.SpeakerService;
import com.zhiyou100.service.VideoService;
import com.zhiyou100.util.DateUtil;

@Controller
@RequestMapping("/admin/video")
public class AdminVideoController {
	@Autowired
	private VideoService vs;
	@Autowired
	private CourseService cs;
	@Autowired
	private SpeakerService ss;
	
	private DateUtil dbutil;
	@RequestMapping("/manage")
	public String videoManege(Integer pageNum,Model mo,HttpSession session,Video video) {
		int pageSize = 6;
		Integer p = (Integer) session.getAttribute("pages");
		if(pageNum==null) {
			pageNum=1;

		}
		if(pageNum<1) {
			pageNum=1;
		}
		if(p!=null) {
			if(pageNum>p) {
				pageNum = p;
			}
		}
		if(video!=null) {
			PageInfo<Vi> pageInfo = vs.selectByContent(pageNum,pageSize,video);
			PageInfo<Video> page = vs.selectByContent1(pageNum, pageSize,video);

			List<Vi> vis = pageInfo.getList();
			List<Course> courses = cs.selectCourses();
			List<Speaker> speakers = ss.selectSpeakerAll();
			session.setAttribute("speakers", speakers);
			session.setAttribute("courses", courses);
			session.setAttribute("pages", page.getPages());
			mo.addAttribute("videos", vis);
			mo.addAttribute("vid", video);
			session.setAttribute("pageNum", page.getPageNum());
		}else {
			
			PageInfo<Vi> pageInfo = vs.selectVideoAllAdmin(pageNum,pageSize);
			PageInfo<Video> page = vs.selectVideoAllAdmin1(pageNum, pageSize);
			
			List<Course> courses = cs.selectCourses();
			List<Speaker> speakers = ss.selectSpeakerAll();
			session.setAttribute("speakers", speakers);
			session.setAttribute("courses", courses);

			List<Vi> vis = pageInfo.getList();
			
			session.setAttribute("pageNum", page.getPageNum());
			session.setAttribute("pages", page.getPages());
			mo.addAttribute("videos", vis);
		}
		
		return "forward:/back/video/video_list.jsp";
	}
	
	@RequestMapping("/edit")
	public String edit(Integer id,Model mo,HttpSession session) {
		Video video = vs.selectVideo(id);
		Vi vi = new Vi();
		vi.setTime(DateUtil.getTime(video.getVideo_length()));
		vi.setId(video.getId());
		vi.setVideo_title(video.getVideo_title());
		vi.setVideo_image_url(video.getVideo_image_url());
		vi.setVideo_url(video.getVideo_url());
		vi.setVideo_descr(video.getVideo_descr());
		vi.setInsert_time(video.getInsert_time());
		vi.setUpdate_time(video.getUpdate_time());
		vi.setVideo_play_times(video.getVideo_play_times());
		vi.setSpeaker_id(video.getSpeaker().getId());
		vi.setCourse_id(video.getCourse().getId());
		mo.addAttribute("video", vi);
		Date date = new Date();
		long time = date.getTime();
		session.setAttribute("time", time);
		return "forward:/back/video/edit_video.jsp";
	}
	
	@RequestMapping("/update")
	private String update(Video video,String time,HttpSession session,String randomCode) {
		Integer pageNum = (Integer) session.getAttribute("pageNum");
		video.setVideo_length(DateUtil.setTime(time));
		video.setSpeaker_id(video.getSpeaker_id());
		video.setCourse_id(video.getCourse_id());
		Long checkTime = (Long) session.getAttribute("time");
		if(checkTime==null) {
			return "redirect:/admin/course/manage?pageNum="+pageNum;
		}
		if(checkTime==Long.parseLong(randomCode)) {
			int i = vs.update(video);
			session.setAttribute("time", null);	
		}
		return "redirect:/admin/video/manage?pageNum="+pageNum;
	}
	
	@RequestMapping("/forAddVideo")
	public String addVideo(HttpSession session) {
		Date date = new Date();
		long time = date.getTime();
		session.setAttribute("time", time);
		return "forward:/back/video/edit_video.jsp";
	}
	
	@RequestMapping("/addVideo")
	public String add(Video video,String time,HttpSession session,String randomCode) {
		Integer pageNum = (Integer) session.getAttribute("pageNum");
		Long checkTime = (Long) session.getAttribute("time");
		if(checkTime==null) {
			return "redirect:/admin/course/manage?pageNum="+pageNum;
		}
		if(checkTime==Long.parseLong(randomCode)) {
			video.setVideo_length(DateUtil.setTime(time));
			video.setSpeaker_id(video.getSpeaker_id());
			video.setCourse_id(video.getCourse_id());
			int i = vs.addVideo(video);
			session.setAttribute("time", null);	
		}	
		return "redirect:/admin/video/manage?pageNum="+pageNum;
	}
	@RequestMapping("/deleteVideo")
	public String deleteVideo(Integer id,HttpSession session) {
		if(id!=null) {
			int i = vs.deleteVideo(id);
		}
		Integer pageNum = (Integer) session.getAttribute("pageNum");
		return "redirect:/admin/video/manage?pageNum="+pageNum;
	}
	@RequestMapping("/queryDelete")
	@ResponseBody
	public Integer queryDelete(String ids,HttpSession session) {
		int pageSize = 6;
		String[] strings = ids.split(",");
		for (String string : strings) {
			vs.deleteVideo(Integer.parseInt(string));
		}
		Integer pageNum = (Integer) session.getAttribute("pageNum");
		PageInfo<Video> page = vs.selectVideoAllAdmin1(pageNum, pageSize);
		Integer pages = page.getPages();
		if(pageNum>pages) {
			pageNum = pages;
		}
		return pageNum;
		
	}
}
